package edu.ybu.manage.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import edu.ybu.manage.pojo.Entor;
import edu.ybu.manage.service.EntorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/student")
public class EntorController {
    @Autowired
    private EntorService entorService;


    @GetMapping("/selectAlla")
        public Map<String,Object> selectAll(){
            List<Entor> entors = entorService.list();
            Map<String, Object> res = new HashMap<String, Object>();
            res.put("code", 0);
            res.put("msg", "");
            res.put("count", entors.size());
            res.put("data",entors );
            return  res;
        }


    /**
     * 条件查询
     * @param studentnamea
     * @return
     */
    @GetMapping("/selectByConditiona")
    public Map<String,Object> selectByCondition(String studentnamea){


//    设置条件查询
        LambdaQueryWrapper<Entor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Entor::getId);
        queryWrapper.like(Entor::getStudentnamea,studentnamea);
        List<Entor> entors = entorService.list(queryWrapper);
        Map<String, Object> res = new HashMap<String, Object>();
        res.put("code", 0);
        res.put("msg", "");
        res.put("count", entors.size());
        res.put("data",entors );
        return  res;

    }

    /**
     * 增加信息

     * @return
     */
    @GetMapping("/insertStudenta")
    public Integer insertStudent(String json){
        ObjectMapper objectMapper = new ObjectMapper();
        Entor entor = null;
        try {
            entor= objectMapper.readValue(json,Entor.class);
        } catch (JsonProcessingException e) {
            System.out.println("json转换失败");
        }
        if (entor==null){
            return 0;
        }
        return entorService.insert(entor);
    }




    /**
     * 删除功能
     * @param
     * @return
     */
    @GetMapping("/deleteStudenta")
    public Integer delectUsers(String json){
        ObjectMapper objectMapper = new ObjectMapper();
        Entor entor = null;
        try {
            entor= objectMapper.readValue(json,Entor.class);
        } catch (JsonProcessingException e) {
            System.out.println("json转换失败");
        }
        if (entor==null){
            return 0;
        }
        return entorService.delect(entor.getId());

    }

    /**
     * 编辑用户
     * @return
     */
    @GetMapping("/updateByIda")
    public Integer updateById(String json){
        ObjectMapper objectMapper = new ObjectMapper();
        Entor entor = null;
        try {
            entor=objectMapper.readValue(json, Entor.class);
        } catch (JsonProcessingException e) {
            System.out.println("json转换失败");
        }
        return entorService.update(entor);
    }
}
